package com.medical.smartcare.activity;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.medical.smartcare.R;
import com.medical.smartcare.asynctask.WebApiAsyncTask;

public class LoginActivity extends Activity
{
	private static final String TAG = LoginActivity.class.getSimpleName();

	private Button btnRegister, btnLogin;
	private EditText etName, etEmail, etPassword;
	private TextView tvMsg;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		try
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_login);

			btnRegister = (Button) findViewById(R.id.btnRegister);
			btnLogin = (Button) findViewById(R.id.btnLogin);
			etName = (EditText) findViewById(R.id.etName);
			etEmail = (EditText) findViewById(R.id.etEmail);
			etPassword = (EditText) findViewById(R.id.etPassword);
			tvMsg = (TextView) findViewById(R.id.tvMsg);

			btnRegister.setOnClickListener(btnOnClick);
			btnLogin.setOnClickListener(btnOnClick);
		}
		catch (Exception ex)
		{
			Log.e(TAG, ex.toString());
		}
	}

	private View.OnClickListener btnOnClick = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			try
			{
				switch (v.getId())
				{
					case R.id.btnRegister:
					{
						JSONObject param = new JSONObject();
						param.put("name", etName.getText().toString());
						param.put("email", etEmail.getText().toString());
						param.put("password", etPassword.getText().toString());

						WebApiAsyncTask webApiAsyncTask = new WebApiAsyncTask();
						JSONObject jsonResult = webApiAsyncTask.execute(WebApiAsyncTask.url_Register, param.toString()).get();

						if (jsonResult != null)
						{
							Log.d(TAG, jsonResult.toString());

							if (jsonResult.getBoolean("error"))
							{
								tvMsg.setText(jsonResult.getString("message"));
							}
							else
							{
								tvMsg.setText(jsonResult.getString("message"));
							}
						}
						else
						{
							Log.e(TAG, "jsonResult is null!");
							tvMsg.setText("Result is null!");
						}
					}
					break;

					case R.id.btnLogin:
					{
						JSONObject param = new JSONObject();
						param.put("email", etEmail.getText().toString());
						param.put("password", etPassword.getText().toString());

						WebApiAsyncTask webApiAsyncTask = new WebApiAsyncTask();
						JSONObject jsonResult = webApiAsyncTask.execute(WebApiAsyncTask.url_Login, param.toString()).get();

						if (jsonResult != null)
						{
							Log.d(TAG, jsonResult.toString());

							if (jsonResult.getBoolean("error"))
							{
								tvMsg.setText(jsonResult.getString("message"));
							}
							else
							{
								// tvMsg.setText("name: " + jsonResult.getString("name") + "\nemail: " + jsonResult.getString("email") + "\napiKey: " + jsonResult.getString("apiKey") + "\ncreatedAt: "
								// + jsonResult.getString("createdAt"));

								Intent intent = new Intent();
								intent.setClass(getApplicationContext(), ManageActivity.class);
								Bundle bundle = new Bundle();
								bundle.putString("Name", jsonResult.getString("name"));
								bundle.putString("Email", jsonResult.getString("email"));
								bundle.putString("ApiKey", jsonResult.getString("apiKey"));
								bundle.putString("CreateDates", jsonResult.getString("createdAt"));
								intent.putExtras(bundle);
								startActivity(intent);
							}
						}
						else
						{
							Log.e(TAG, "jsonResult is null!");
							tvMsg.setText("Result is null!");
						}

					}
					break;

					default:
						Log.d(TAG, "None button");
					break;
				}
			}
			catch (Exception ex)
			{
				Log.e(TAG, ex.toString());
			}
		}
	};
}
